package nsqproc

import (
	"dhfshop/domain/im"
	"dhfshop/models/im"
	"dhfshop/models/isv"
	"encoding/json"
	"github.com/astaxie/beego"
)

type NsqMsgHeader struct {
	AppId   string `json:"app_id"`
	ToUser  string `json:"to_user"`
	MsgType string `json:"msgtype"`
}

//审核消息结构
type AuditMsgBody struct {
	Xid    string `json:"xid"`    //formid_cache业务单据号
	Name   string `json:"name"`   //名称
	Remark string `json:"remark"` //备注
	Tips   string `json:"tips"`   //温馨提示
}

//订购成功通知
type SubscribeMsgBody struct {
	Xid       string `json:"xid"`        //formid_cache业务单据号
	Content   string `json:"content"`    //订购内容
	OrderTime string `json:"order_time"` //订购时间
	Tips      string `json:"tips"`       //温馨提示
}

type NsqMessage struct {
	NsqMsgHeader
	*AuditMsgBody
	*SubscribeMsgBody
}

func NsqRegisterFunc(body []byte) error {
	var nsqMsg NsqMessage
	if err := json.Unmarshal(body, &nsqMsg); err != nil {
		beego.Debug(err.Error())
	}
	beego.Debug("nsq message:", string(body))
	switch nsqMsg.NsqMsgHeader.MsgType {
	case "audit":
		proAuditMsg(&nsqMsg)
	case "sellerorder":
		proOrderSuccessMsg(&nsqMsg)
	case "isvorder":
		proIsvOrderSuccessMsg(&nsqMsg)
	default:

	}
	return nil
}

// 代理商申请审核结果通知
func proAuditMsg(nsqMsg *NsqMessage) error {
	appid := nsqMsg.NsqMsgHeader.AppId
	pubTitleId := "AT0146"
	custAccount, err := isv.GetCustAccountByAppId(appid)
	if err != nil {
		return err
	}
	templateId, err := imlayer.GetTemplateId(custAccount.PartnerId, appid, pubTitleId)
	if err != nil {
		return err
	}
	beego.Debug("nsq templateId:", templateId)
	page := "pages/distribution/add/index"
	formId, err := im.GetFormIdByOpenId(nsqMsg.NsqMsgHeader.ToUser, "audit", nsqMsg.AuditMsgBody.Xid)
	if err != nil {
		return err
	}
	err = imlayer.AuditResultTempMsg(custAccount.AccessToken, page, nsqMsg.NsqMsgHeader.ToUser, templateId, formId,
		nsqMsg.AuditMsgBody.Name, nsqMsg.AuditMsgBody.Remark, nsqMsg.AuditMsgBody.Tips)
	if err != nil {
		return err
	}
	return nil
}

// 报名活动,签到后,通知消息
func proOrderSuccessMsg(nsqMsg *NsqMessage) error {
	appid := nsqMsg.NsqMsgHeader.AppId
	pubTitleId := "AT0087"
	custAccount, err := isv.GetCustAccountByAppId(appid)
	if err != nil {
		return err
	}
	templateId, err := imlayer.GetTemplateId(custAccount.PartnerId, appid, pubTitleId)
	if err != nil {
		return err
	}
	beego.Debug("nsq templateId:", templateId)
	page := "pages/huodong/list/index"
	formId, err := im.GetFormIdByOpenId(nsqMsg.NsqMsgHeader.ToUser, "sellerorder", nsqMsg.SubscribeMsgBody.Xid)
	if err != nil {
		return err
	}
	err = imlayer.OrderSuccessTempMsg(custAccount.AccessToken, page, nsqMsg.NsqMsgHeader.ToUser, templateId, formId,
		nsqMsg.SubscribeMsgBody.Content, nsqMsg.SubscribeMsgBody.OrderTime, nsqMsg.SubscribeMsgBody.Tips)
	if err != nil {
		return err
	}
	return nil
}

// ISV服务订购成功通知
func proIsvOrderSuccessMsg(nsqMsg *NsqMessage) error {
	sellerAppID := beego.AppConfig.String("SellerAppID")
	adminAuthorize, err := isv.GetAuthorizeByAppId(sellerAppID)
	if err != nil {
		return err
	}
	templateId := "A8H9rhgIfiWVFy0AT7bcF-aXyEM8vLOSBUsQFfaDdIU"
	page := "pages/huodong/list/index"
	formId, err := im.GetFormIdByOpenId(nsqMsg.NsqMsgHeader.ToUser, "isvorder", nsqMsg.SubscribeMsgBody.Xid)
	if err != nil {
		return err
	}
	err = imlayer.OrderSuccessTempMsg(adminAuthorize.AccessToken, page, nsqMsg.NsqMsgHeader.ToUser, templateId, formId,
		nsqMsg.SubscribeMsgBody.Content, nsqMsg.SubscribeMsgBody.OrderTime, nsqMsg.SubscribeMsgBody.Tips)
	if err != nil {
		return err
	}
	return nil
}
